<template>
  <!-- 寻物发布 -->
  <view>
    <uni-file-picker limit="3" :imageStyles="imageStyles" @select="uploading" @delete="fileDelete">
      <uni-icons type="plusempty" size="38rpx" color="#707070"></uni-icons>
    </uni-file-picker>
  </view>
</template>

<script>
export default {
  props: ['limit'],
  data() {
    return {
      // 上传图片地址
      fileList: [],
      // 图片上传样式
      imageStyles: {
        width: '120rpx',
        height: '120rpx',
        border: {
          radius: '20rpx',
        }
      },
    }
  },
  methods: {

    // 图片上传
    uploading(file) {
      uni.uploadFile({
        url: 'https://songcan.xyct1688.com/api/upload/xytimg',
        filePath: file.tempFilePaths[0],
        name: 'file',
        success: (res) => {
          if (JSON.parse(res.data).code == -1) {
            uni.showToast({
              title: '图片违规，请上传合法图片',
              icon: 'none',
              duration: 2000
            })
            this.fileList.pop()
            return
          }
          let path = JSON.parse(res.data).data.pic_path
          this.fileList.push(path)
          this.$emit('change', this.fileList)
        },
        fail: e => {
        },
      });
    },
    // 删除图片
    fileDelete(file) {
      let index = this.fileList.indexOf(file.tempFilePath)
      this.fileList.splice(index, 1)
      this.$emit('change', this.fileList)

    },

  }
}
</script>

<style lang="less" scoped></style>